package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminUserService;
import com.cskaoyan.market.service.impl.AdminUserServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/6/17 15:40
 * @Version 1.0
 */
@WebServlet("/admin/user/*")
public class AdminUserController extends HttpServlet {

    AdminUserService adminUserService = new AdminUserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/user/", "");
        if ("update".equals(op)) {
            update(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String reqBody = req.getReader().readLine();
        MarketUser marketUser = JacksonUtil.getObjectMapper().readValue(reqBody, MarketUser.class);
        Integer num = adminUserService.update(marketUser);
        //num=1 表示正常更新成功
        if (num == 1) {
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s);
            //num=2 说明密码为空
        } else if (num == 2) {
            Object o = ResponseUtil.fail(MarketConstant.WORNG_PASSISEMPTY,"密码不能为空");
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
            //num=3 说明电话号码格式不对
        } else if (num == 3) {
            Object o = ResponseUtil.fail(MarketConstant.WRONG_MOBILE,"电话号码不能为空");
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/user/", "");
        if("list".equals(op)){
            list(req, resp);
        }else if ("detail".equals(op)) {
            detail(req, resp);
        }
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketUser marketUser = adminUserService.detail(id);
        Object ok = ResponseUtil.ok(marketUser);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);

        resp.getWriter().println(s);
    }

    //需要展示出当前商城的所有用户
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收请求参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String username = req.getParameter("username");
        String mobile = req.getParameter("mobile");
        List<MarketUser> userList = adminUserService.list(page, limit, sort, order, username, mobile);

        Object o = ResponseUtil.okList(userList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }
}
